/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.wx.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.wx.entity.WxAccount;
import com.thinkgem.jeesite.modules.wx.service.WxAccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.wx.entity.WxMenu;
import com.thinkgem.jeesite.modules.wx.service.WxMenuService;

/**
 * 微信菜单Controller
 * @author Rocky
 * @version 2016-06-22
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxMenu")
public class WxMenuController extends BaseController {

	@Autowired
	private WxMenuService wxMenuService;
	@Resource
	WxAccountService wxAccountService;
	
	@ModelAttribute
	public WxMenu get(@RequestParam(required=false) String id) {
		WxMenu entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wxMenuService.get(id);
		}
		if (entity == null){
			entity = new WxMenu();
		}
		return entity;
	}
	
	@RequiresPermissions("wx:wxMenu:view")
	@RequestMapping(value = {"list", ""})
	public String list(WxMenu wxMenu, HttpServletRequest request, HttpServletResponse response, Model model) {

		List<WxMenu> list = wxMenuService.findList(wxMenu);
		WxAccount account = new WxAccount();
		account.setCompany(UserUtils.getUser().getCompany());
		List<WxAccount> accounts = wxAccountService.findList(account);

		if(accounts != null && accounts.size() > 0){
			account = accounts.get(0);
			model.addAttribute("account", account);
		}

		model.addAttribute("accounts", accounts);
		model.addAttribute("list", list);
		return "modules/wx/wxMenuList";
	}

	@RequiresPermissions("wx:wxMenu:view")
	@RequestMapping(value = "form")
	public String form(WxMenu wxMenu, Model model) {
		if (wxMenu.getParent()!=null && StringUtils.isNotBlank(wxMenu.getParent().getId())){
			wxMenu.setParent(wxMenuService.get(wxMenu.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(wxMenu.getId())){
				WxMenu wxMenuChild = new WxMenu();
				wxMenuChild.setParent(new WxMenu(wxMenu.getParent().getId()));
				List<WxMenu> list = wxMenuService.findList(wxMenu); 
				if (list.size() > 0){
					wxMenu.setSort(list.get(list.size()-1).getSort());
					if (wxMenu.getSort() != null){
						wxMenu.setSort(wxMenu.getSort() + 30);
					}
				}
			}
		}
		if (wxMenu.getSort() == null){
			wxMenu.setSort(30);
		}
		model.addAttribute("wxMenu", wxMenu);
		return "modules/wx/wxMenuForm";
	}

	@RequiresPermissions("wx:wxMenu:edit")
	@RequestMapping(value = "save")
	public String save(WxMenu wxMenu, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wxMenu)){
			return form(wxMenu, model);
		}
		wxMenuService.save(wxMenu);
		addMessage(redirectAttributes, "保存微信菜单成功");
		return "redirect:"+Global.getAdminPath()+"/wx/wxMenu/?repage";
	}
	
	@RequiresPermissions("wx:wxMenu:edit")
	@RequestMapping(value = "delete")
	public String delete(WxMenu wxMenu, RedirectAttributes redirectAttributes) {
		wxMenuService.delete(wxMenu);
		addMessage(redirectAttributes, "删除微信菜单成功");
		return "redirect:"+Global.getAdminPath()+"/wx/wxMenu/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<WxMenu> list = wxMenuService.findList(new WxMenu());
		for (int i=0; i<list.size(); i++){
			WxMenu e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}